<template>
  <div class="app-container drag-container">
    <el-row :gutter="0">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="课程名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            clearable
            placeholder="请输入课程名称"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="courseList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="课程名称" align="center" prop="courseName" />
        <el-table-column label="课程编码" align="center" prop="courseCode" />
        <el-table-column label="课程性质" align="center" prop="courseNature" />
        <el-table-column
          label="课程属性"
          align="center"
          prop="courseAttribute"
        />
        <el-table-column label="课程学分" align="center" prop="courseCredit" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="goTrain(scope.row.id)"
              >进入训练</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <!-- 对话框 -->
      <el-dialog
        class="my-el-dialog"
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <!-- 选择题库 -->
          <el-form-item label="选择题库" prop="typeId">
            <el-select
              v-model="form.typeId"
              placeholder="请选择题库"
              filterable
              size="small"
              style="width: 100%"
              @change="handleTypeId()"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 选择题型模板 -->
          <el-form-item label="题型模板" prop="tempId">
            <el-select
              v-model="form.tempId"
              placeholder="请选择题型模板"
              filterable
              size="small"
              style="width: 100%"
              @change="handleTempId()"
            >
              <el-option
                v-for="item in tempOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm()">确 定</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { listCourse } from "@/api/train/course";
import {
  getQuestionBank,
  getQuestionBankTemplate,
  getQuestionBankCout,
  getRandomQuestion,
} from "@/api/train/questionbank";
export default {
  name: "Course",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 题库选项
      typeOptions: [],
      // 题型模板选项
      tempOptions: [],
      // 表单参数
      form: {
        typeId: null, //题库id
        tempId: null, //题型模板id
        beanName: null, //题型编码
      },
      // 表单校验
      rules: {
        typeId: [{ required: true, message: "请选择题库", trigger: "blur" }],
        tempId: [
          { required: true, message: "请选择题型模板", trigger: "blur" },
        ],
      },
      // 显示搜索条件
      showSearch: true,
      // 基本课程管理表格数据
      courseList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取课程列表
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 按钮-进入训练
    goTrain(id) {
      this.open = true;
      getQuestionBank(id).then((response) => {
        console.log(response);
        this.typeOptions = response;
      });
    },
    // 按钮-取消
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    // 表单提交
    submitForm() {
      // 对整个表单进行校验，如果校验通过则执行传入的回调函数
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.typeId && this.form.tempId) {
            // 随机获取题
            getRandomQuestion(
              this.form.typeId,
              this.form.tempId,
              this.form.beanName
            ).then((response) => {
              console.log(response);
              Cookies.set(
                "trianToken",
                JSON.stringify({
                  typeId: this.form.typeId,
                  tempId: this.form.tempId,
                  beanName: this.form.beanName,
                }),
                { expires: 1 }
              );
              Cookies.set("train", JSON.stringify(response), { expires: 1 });
              // this.$store.commit('train/GET_TRAIN_INFO', { a: 1 })
              this.$router.push("/mytrain");
            });
            // 获取题目总数
            // getQuestionBankCout(this.form.typeId, this.form.tempId).then((response) => {
            // 	console.log(response)
            // })
          }
        }
      });
    },
    // 题库选择
    handleTypeId() {
      this.form.tempId = null;
      getQuestionBankTemplate(this.form.typeId).then((response) => {
        console.log(response);
        this.tempOptions = response;
      });
    },
    // 题型模板选择
    handleTempId(id) {
      let obj = this.tempOptions.find((item) => item.id == this.form.tempId);
      this.form.beanName = obj.beanName;
    },
  },
};
</script>
<style lang="scss" scoped>
.my-el-dialog {
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 10%;
  left: calc(50% - 271.25);
  // .el-dialog {
  // 	margin-top: 0 !important;
  // }
  // .el-dialog__body {
  // 	flex: 1;
  // 	overflow: auto;
  // }
}
</style>
